<?php
class Url {
	private $url;
	private $ssl;
	
	public function __construct($url, $ssl = '') {
		$this->url = $url;
		$this->ssl = $ssl;
	}
		
	public function link($route = '', $args = '', $connection = 'NONSSL') {
		if ($connection ==  'NONSSL') {
			$url_f = $this->url;	
		} else {
			$url_f = $this->ssl;	
		}
		if($route == ''){
			if($args == ''){
				return rtrim($url_f, '/');
			}
			$route = "common/home";
		}
		
		$args = ltrim($args, '&');
		if(strlen($args) > 0){
			return $url_f . 'index.php?route=' . $route . '&' . $args;
		}else{
			return $url_f . 'index.php?route=' . $route;
		}
	}
	
	public function linkRewrite($route = '', $args = '', $connection = 'NONSSL') {
		if ($connection ==  'NONSSL') {
			$url_f = $this->url;	
		} else {
			$url_f = $this->ssl;	
		}
		if(strlen($route) == 0){
			return rtrim($url_f, '/');
		}
		
		$args = ltrim($args, '&');
		if(strlen($args) > 0){
			return $url_f . 'index.php?route=' . $route . '&' . $args;
		}else{
			return $url_f . 'index.php?route=' . $route;
		}
	}
	
	public function linkCategory($id, $name = '') {
		return $this->url . $this->word2url($name) . "-c" . $id;
	}
	
	public function linkProduct($id, $name = '') {
		return $this->url . $this->word2url($name) . "-p" . $id;
	}
	
	public function linkLang($lang, $route, $connection) {
		if ($connection ==  'NONSSL') {
			$url_f = $this->url;	
		} else {
			$url_f = $this->ssl;	
		}
		if(strlen($route) == 0){
			return $url_f . $lang;
		}else{
			return $url_f . $lang . "/" . $route;
		}
	}
	
	public function linkCurrency($currency, $route, $connection) {
		if ($connection ==  'NONSSL') {
			$url_f = $this->url;	
		} else {
			$url_f = $this->ssl;	
		}
		if(strlen($route) == 0){
			return $url_f . "index.php?route=common/home&currency_code=" . $currency;
		}else{
			return $url_f . $route . "?currency_code=" . $currency;
		}
	}
	
	public function word2url($str = ''){
		$str = trim($str);
		if(strlen($str) > 0){
			$str = preg_replace("/\s+/", "-", $str);
		}
		return $str;
	}
}
?>